/*    */ package cn.zzuisa.news.servlet;
/*    */ 
/*    */ import cn.zzuisa.news.entity.User;
/*    */ import cn.zzuisa.news.enums.CommonEnums;
/*    */ import cn.zzuisa.news.form.RegisterForm;
/*    */ import cn.zzuisa.news.service.UserService;
/*    */ import cn.zzuisa.news.service.impl.UserServiceImpl;
/*    */ import cn.zzuisa.news.util.JsonUtil;
/*    */ import cn.zzuisa.news.util.ResultVOUtil;
/*    */ import com.google.gson.Gson;
/*    */ import com.google.gson.JsonObject;
/*    */ import com.google.gson.JsonParser;
/*    */ import java.io.IOException;
/*    */ import javax.servlet.ServletException;
/*    */ import javax.servlet.annotation.WebServlet;
/*    */ import javax.servlet.http.HttpServlet;
/*    */ import javax.servlet.http.HttpServletRequest;
/*    */ import javax.servlet.http.HttpServletResponse;
/*    */ import javax.servlet.http.HttpSession;
/*    */ 
/*    */ 
/*    */ @WebServlet(name = "login", urlPatterns = {"/login"}, asyncSupported = true)
/*    */ public class LoginServlet
/*    */   extends HttpServlet
/*    */ {
/* 26 */   private UserService userServiceImpl = new UserServiceImpl();
/*    */ 
/*    */ 
/*    */   
/*    */   protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
/* 31 */     String method = req.getParameter("method");
/* 32 */     if (method == null)
/* 33 */       req.getRequestDispatcher("WEB-INF/ftl/user/login.ftl").forward(req, resp); 
/* 34 */     if (method.equals("login")) {
/*    */       
/* 36 */       String c = JsonUtil.getStrResponse(req);
/*    */       
/* 38 */       JsonObject user = (new JsonParser()).parse(c).getAsJsonObject();
/*    */       
/* 40 */       User auth = this.userServiceImpl.login(user.get("username").getAsString(), user.get("password").getAsString());
/* 41 */       if (auth != null) {
/* 42 */         if (auth.getState().intValue() == 1) {
/* 43 */           resp.getWriter().println(JsonUtil.setData(ResultVOUtil.failed(CommonEnums.USER_LOCKED)));
/*    */         } else {
/* 45 */           HttpSession session = req.getSession();
/* 46 */           session.setMaxInactiveInterval(21600);
/* 47 */           session.setAttribute("user", auth);
/* 48 */           resp.getWriter().println(JsonUtil.setData(ResultVOUtil.success(auth)));
/*    */         } 
/*    */       } else {
/*    */         
/* 52 */         resp.getWriter().println(JsonUtil.setData(ResultVOUtil.failed(CommonEnums.LOGIN_FAIL)));
/*    */       } 
/*    */     } 
/* 55 */     if (method.equals("loginforAndroid")) {
/* 56 */       String username = req.getParameter("username");
/* 57 */       String password = req.getParameter("password");
/* 58 */       User auth = this.userServiceImpl.login(username, password);
/* 59 */       System.out.println("auto:" + auth);
/* 60 */       if (auth != null) {
/* 61 */         if (auth.getState().intValue() == 1) {
/* 62 */           resp.getWriter().println(JsonUtil.setData(ResultVOUtil.failed(CommonEnums.USER_LOCKED)));
/*    */         } else {
/* 64 */           HttpSession session = req.getSession();
/* 65 */           session.setMaxInactiveInterval(21600);
/* 66 */           session.setAttribute("user", auth);
/* 67 */           resp.getWriter().println(JsonUtil.setData(ResultVOUtil.success(auth)));
/*    */         } 
/*    */       } else {
/*    */         
/* 71 */         resp.getWriter().println(JsonUtil.setData(ResultVOUtil.failed(CommonEnums.LOGIN_FAIL)));
/*    */       } 
/*    */     } 
/* 74 */     if (method.equals("register")) {
/*    */       
/* 76 */       String cs = JsonUtil.getStrResponse(req);
/* 77 */       JsonObject reg = (new JsonParser()).parse(cs).getAsJsonObject();
/* 78 */       RegisterForm registerForm = (RegisterForm)(new Gson()).fromJson(reg, RegisterForm.class);
/* 79 */       System.out.println("form" + registerForm.toString());
/* 80 */       User valid = null;
/* 81 */       valid = this.userServiceImpl.findByUsername(registerForm.getUsername());
/* 82 */       if (valid == null) {
/* 83 */         if (this.userServiceImpl.register(registerForm)) {
/* 84 */           resp.getWriter().println(JsonUtil.setData(ResultVOUtil.success(CommonEnums.SUCCESS)));
/*    */         } else {
/* 86 */           resp.getWriter().println(JsonUtil.setData(ResultVOUtil.failed(CommonEnums.PARAM_ERROR)));
/*    */         } 
/*    */       } else {
/* 89 */         resp.getWriter().println(JsonUtil.setData(ResultVOUtil.failed(CommonEnums.USERNAME_HAS_EXIST)));
/*    */       } 
/*    */     } 
/* 92 */     if (method.equals("logout")) {
/* 93 */       HttpSession session = req.getSession();
/* 94 */       session.removeAttribute("user");
/* 95 */       resp.sendRedirect("index");
/*    */     } 
/*    */   }
/*    */ }


/* Location:              /Users/frank/Desktop/admin/WEB-INF/classes/!/cn/zzuisa/news/servlet/LoginServlet.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.0.7
 */